<template>
  <div>
     <pre v-text="html123()">

      </pre>
  </div>
</template>
<script>

function type2tstype(texttype) {
  if (texttype == 'integer') {
    return 'number';
  } else if (texttype == 'text') {
    return 'string';
  }
}

function underlineToCamel(str) {
  return str?.replace(/_([a-z])/g, function (match, group1) {
    return group1.toUpperCase();
  });
}

export default {
  data() {
    return {
      type2tstype,
      underlineToCamel,

    };
  },

  props: {
    columns: { type: [Array] },
    tableName: { type: [String] },
  },
  methods: {
    html123() {
      var colhtml=''
      for (let i = 0; i < this.$props.columns.length; i++) {
        var colitem=this.$props.columns[i]
        colhtml+=`
                 <el-form-item
                        label="${colitem.colkey}" prop="${colitem.colkey}"   align="center">
                        <div slot="label"><span>${colitem.colkey}</span></div>
                        <el-input  v-model="formData['${colitem.colkey}']"
                        clearable size="mini" style="width:120px"/>
                </el-form-item>`
      }






      return `//${underlineToCamel(this.$props.tableName)}.modify.vue
            <template>
               <el-form
                       ref="formref"
      :model="formData"
      label-width="auto"
      :inline="true"
                >
                ${colhtml}

                </el-form>
              </template>
              <script >
                  import axios from 'axios';
                  import { formData, formMethod, PageData, uniqWithKey } from '@/utils/formcommon';
              import { openLayer } from '@/utils/layerdialog/layerdialog';
          import { alertConfirm,alertWithPayload } from '@base/utils';
import { lodashFillData } from '@/utils/lodashUtil';
import {${underlineToCamel(this.$props.tableName)}} from './${underlineToCamel(this.$props.tableName)}.json.js'
                 export default{
                 data(){
                   return {
                   total:0,
                   formData:{
                     ...${underlineToCamel(this.$props.tableName)},
                    ...PageData,
                   },
                   };
                 },
                 methods:{
                 ...formMethod,
                     async initByDialog(_data){
                           if(_data?.row?.id){
                                var result=await axios.get(\`/api/${this.$props.tableName}/getbyid?id=\${_data?.row?.id}\`,{...this.queryParams}).then(res=>res.data);
                                 var _data2=result?.data;
                                 lodashFillData(this.formData,_data2)
                                 console.log("=>[_data2]", _data2);
                           }
                          },
                          async submitByDialog(){
                           //validate
                            //data operation
                            //ajax 提交
                            var url='/api/${this.$props.tableName}/add'
                            if(this.formData?.id){
                             url='/api/${this.$props.tableName}/update'
                            }
                           var result=await axios.post(url,{...this.formData}).then(res=>res.data)
                           alertWithPayload(result)
                            return result
                          },
                 },
               };
              <\/script>`;
    },
  },
};
</script>
